package it.polimi.politics.web.beans;

import java.util.ArrayList;
import java.util.List;

import it.polimi.politics.dto.ReferendumDTO;
import it.polimi.politics.dto.UtentiDTO;
import it.polimi.politics.dto.VotoreferendumDTO;
import it.polimi.politics.managers.ReferendumManager;
import it.polimi.politics.managers.UserManager;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


@ManagedBean(name="referendumBean")
@ViewScoped
public class ReferendumBean {

	@EJB
	private ReferendumManager referendumMgr;
	
	@EJB
	private UserManager userMgr;
	
	private List<ReferendumDTO> referendumProposti;
	
	private List<ReferendumDTO> referendumApprovati;
	
	private ReferendumDTO referendum;
	
	private String referendumId;
	
	private int inRefid;
	
	
	
	
	@PostConstruct
    public void init() {
	
	referendumId = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");	
		
	referendum = new ReferendumDTO();
	
	
    
	this.setReferendumProposti(referendumMgr.visualizzaProposte(userMgr.getUtente()));
	
	this.setReferendumApprovati(referendumMgr.visualizzaApprovati());
	
	
	
    }
	
	public String CreaReferendum(){
		
		referendum.setStato(0);
		referendumMgr.creaProposta(referendum);
		
		
		return "index?faces-redirect=true";
	}
	
	
	
public String VotaSi() {
		
		inRefid=Integer.parseInt(referendumId);
		UtentiDTO user=userMgr.getUtente();
		VotoreferendumDTO votoreferendum=new VotoreferendumDTO(inRefid, 1);
		referendumMgr.vota(referendum, votoreferendum, user);
        
		return "home?faces-redirect=true";
	}

public String VotaNo() {
	
	inRefid=Integer.parseInt(referendumId);
	UtentiDTO user=userMgr.getUtente();
	VotoreferendumDTO votoreferendum=new VotoreferendumDTO(inRefid, 0);
	referendumMgr.vota(referendum, votoreferendum, user);
    
	return "home?faces-redirect=true";
}

public String Liker(){
	inRefid=Integer.parseInt(referendumId);
	UtentiDTO user=userMgr.getUtente();
	referendumMgr.likeRef(referendum, user);
    
	return "home?faces-redirect=true";
	
}
		
	public String getTesto(){
		int id = Integer.parseInt(referendumId);
		visualizzaReferendum(id);
		return referendum.getTestoProposta();
	}
	
	public String getTitolo(){
		int id = Integer.parseInt(referendumId);
		visualizzaReferendum(id);
		return referendum.getTitolo();
	}
	
	public int getNumero(){
		int id = Integer.parseInt(referendumId);
		visualizzaReferendum(id);
		return referendum.getIdReferendum();
	}
	
	public void visualizzaReferendum(int idReferendum){
		
		setReferendum(referendumMgr.trovaReferendum(idReferendum));
		
	}
	
	


	public List<ReferendumDTO> getReferendumProposti() {
		return referendumProposti;
	}

	public void setReferendumProposti(List<ReferendumDTO> referendumProposti) {
		this.referendumProposti = referendumProposti;
	}

	public List<ReferendumDTO> getReferendumApprovati() {
		return referendumApprovati;
	}

	public void setReferendumApprovati(List<ReferendumDTO> referendumApprovati) {
		this.referendumApprovati = referendumApprovati;
	}

	public ReferendumDTO getReferendum() {
		return referendum;
	}

	public void setReferendum(ReferendumDTO referendum) {
		this.referendum = referendum;
	}


	
	
	

	
	
	
	
}
